package com.xiaoquan.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xiaoquan.gulimall.product.entity.AttrEntity;
import com.xiaoquan.gulimall.product.service.AttrAttrgroupRelationService;
import com.xiaoquan.gulimall.product.service.AttrService;
import com.xiaoquan.gulimall.product.service.CategoryService;
import com.xiaoquan.gulimall.product.vo.AttrGroupRelationVo;
import com.xiaoquan.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiaoquan.gulimall.product.entity.AttrGroupEntity;
import com.xiaoquan.gulimall.product.service.AttrGroupService;
import com.xiaoquan.common.utils.PageUtils;
import com.xiaoquan.common.utils.R;



/**
 * 属性分组
 *
 * @author xiaoquan
 * @email 1461240780@qq.com
 * @date 2021-06-03 18:06:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    // /product/attrgroup/attr/relation 11、添加属性与分组关联关系
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){

        // 1、查出当前分类下的所有属性分组
        // 2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",vos);
    }

    // /product/attrgroup/{attrgroupId}/attr/relation 10、获取属性分组的关联的所有属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public  R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntities);
    }

    // /product/attrgroup/{attrgroupId}/noattr/relation 13、获取属性分组没有关联的其他属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public  R attrNoRelation(@RequestParam Map<String, Object> params
            ,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        // 所以不是自己new 出来的对象 都要进行判空
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup == null){
            return R.error().put("msg","输入的id,数据库中没有；请重新输入");
        }

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 删除属性与分组的关联
     */
    // /product/attrgroup/attr/relation/delete  12、删除属性与分组的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
